package com.gs.disk.dao.common;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.common.mapper.CommonMapper;
import com.gs.entity.MenuEntity;
import org.apache.ibatis.annotations.Mapper;

import java.io.Serializable;
import java.util.*;

@Mapper
public interface MenuDao extends CommonMapper<MenuEntity, MenuEntity> {

    /**
     * 获取菜单数形结构
     * @return
     */
    default List<MenuEntity> listTree() {
        // 获取一级菜单列表
        QueryWrapper<MenuEntity> wrapperLevel1 = new QueryWrapper<>();
        wrapperLevel1.select("id,path,name,permissions,component,redirect,icon,hidden");
        wrapperLevel1.eq("menu_level", 1);
        List<MenuEntity> menuLevelList = selectList(wrapperLevel1);

        menuLevelList.forEach(menuLevel1 -> {
            // 获取二级
            Long id = menuLevel1.getId();
            QueryWrapper<MenuEntity> wrapperLevel2 = new QueryWrapper<>();
            wrapperLevel2.select("id,path,name,permissions,component,redirect,icon,hidden, parent_id");
            wrapperLevel2.eq("menu_level", 2);
            wrapperLevel2.eq("parent_id", id);
            List<MenuEntity> menuLevel2List = selectList(wrapperLevel2);
            menuLevel1.setChildren(menuLevel2List);
        });

        return menuLevelList;
    }


    /**
     * 获取菜单数形结构
     * @param menuNameArr 菜单列表
     * @param hidden 是否隐藏
     * @return
     */
    default List<MenuEntity> listUserMenuTree(Serializable[] menuNameArr, Boolean hidden) {
        QueryWrapper<MenuEntity> wrapperLevel1 = new QueryWrapper<>();
        wrapperLevel1.select("id,path,name,component,redirect,icon,hidden");
        wrapperLevel1.eq("menu_level", 1);
        wrapperLevel1.eq("hidden", hidden);

        wrapperLevel1.and(w -> {
            for (Serializable menuName : menuNameArr) {
                w.eq("name", menuName);
                w.or();
            }
        });

        List<MenuEntity> menuLevelList = selectList(wrapperLevel1);

        menuLevelList.forEach(menuLevel1 -> {
            // 获取二级
            Long id = menuLevel1.getId();
            QueryWrapper<MenuEntity> wrapperLevel2 = new QueryWrapper<>();
            wrapperLevel2.select("id,path,name,component,redirect,icon,hidden");
            wrapperLevel2.eq("menu_level", 2);
            wrapperLevel2.eq("parent_id", id);

            wrapperLevel2.and(w -> {
                for (Serializable menuName : menuNameArr) {
                    w.eq("name", menuName);
                    w.or();
                }
            });

            List<MenuEntity> menuLevel2List = selectList(wrapperLevel2);
            menuLevel1.setChildren(menuLevel2List);
        });

        return menuLevelList;
    }

    /**
     * 权限列表获取
     * @param menuList 菜单列表
     * @return
     */
    default Set<String> listPermissionSet(List<String> menuList) {
        Set<String> resultSet = new HashSet<>();
        QueryWrapper<MenuEntity> wrapper = new QueryWrapper<>();
        wrapper.select("permissions");
        menuList.stream().forEach(menu -> {
            wrapper.eq("name", menu);
            wrapper.or();
        });

        List<MenuEntity> permissionsList = selectList(wrapper);

        permissionsList.stream().forEach(menu -> {
            MenuEntity menuEntity = Optional.ofNullable(menu).orElse(new MenuEntity());
            String permissions = Optional.ofNullable(menuEntity.getPermissions()).orElse("");
            String[] split = permissions.split(",");

            Arrays.stream(split).forEach(s -> {
                if(ObjectUtil.isNotEmpty(s)) {
                    resultSet.add(s);
                }
            });
        });

        return resultSet;
    }
}